package com.itany.controllers;

import com.itany.service.ExamineService;
import com.itany.service.UserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/examine")
public class ExamineController {
    @Autowired
    ExamineService service;

    @RequestMapping("/findAll")
    public Map<String, Object> findAll(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,
            @RequestParam("name") String name,
            @RequestParam("flag") Integer flag,
            @RequestParam("type") Integer type) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = service.queryByParams(name,flag,type);
        int total = list != null ? list.size() : 0;

        // 如果总数据量小于或等于每页显示的数据条数，则始终从第一页开始，且只显示一页
        if (total <= rows) {
            page = 1;
        } else {
            // 计算总页数，并检查请求的页码是否超出范围
            int totalPages = (int) Math.ceil((double) total / rows);
            if (page > totalPages) {
                page = 1; // 如果请求的页码大于总页数，则重置为第一页
            }
        }

        // 计算起始和结束索引
        int startIndex = (page - 1) * rows;
        int endIndex = Math.min(startIndex + rows, total);

        // 创建结果列表，并填充数据
        if(list!=null){
            List<Map<String, Object>> ref = list.subList(startIndex, endIndex);
            map.put("rows", ref);
        }else{
            map.put("rows",0);
        }
        map.put("total", total);

        return map;


    }

    @RequestMapping("/findServerCompany")
    public ActionResult findServerCompany(@RequestParam("id") Integer id){
        Map<String,Object> map = service.findServerCompany(id);
        return ActionResult.success("查找成功",200,map);
    }

    @RequestMapping("/findServer")
    public ActionResult findServer(@RequestParam("id") Integer id){
        Map<String,Object> map = service.findServer(id);
        return ActionResult.success("查找成功",200,map);
    }

    @RequestMapping("/examineCompany")
    public ActionResult examineCompany(
            @RequestParam("id") Integer id,
            @RequestParam("flag") Integer flag,
            @RequestParam("examineInfo") String examineInfo,
            @RequestParam(value = "managerUserId" , required = false) Integer managerUserId){
        service.examineCompany(id,flag,examineInfo,managerUserId);
        return ActionResult.success("审核成功!",200,null);
    }

    @RequestMapping("/examineServer")
    public ActionResult examineServer(
            @RequestParam("id") Integer id,
            @RequestParam("flag") Integer flag,
            @RequestParam("examineInfo") String examineInfo,
            @RequestParam(value = "managerUserId" , required = false) Integer managerUserId){
        service.examineServer(id,flag,examineInfo,managerUserId);
        return ActionResult.success("审核成功!",200,null);
    }
}
